<?php
namespace Act\Os;
use Lib\ErrorMessage;
use Lib\Common;
class User extends Backend{
	public function doAdd(){
		if(is_post()){
			$user_name = $real_name = $mobile = $password  = $email = $user_desc = $user_group = '';
			extract ( $_POST, EXTR_IF_EXISTS );
			$user_mod = model('#User','adm_');
			$exist = $user_mod->getUserByName($user_name);
			if($exist){
				$this->alert("error",ErrorMessage::NAME_CONFLICT);
			}else if($password=="" || $real_name=="" || $mobile =="" || $email =="" || $user_group <= 0 ){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				list($password,$salt) = $user_mod->genPassword($password);
				$input_data = array ('user_name' => $user_name, 'password' => $password, 'salt' => $salt , 'real_name' => $real_name, 'mobile' => $mobile, 'email' => $email, 'user_desc' => $user_desc, 'user_group' => $user_group );
				$user_id = $user_mod->addUser ( $input_data );
				
				if ($user_id) {
					$input_data['password']="";
					$this->sysLog ( session('user_info')['user_name'], 'ADD', 'User' ,$user_id, json_encode($input_data) );
					return $this->success ('账号添加成功','os/users');
				}else{
					$this->alert("error");
				}
			}
		}

		$this->display('from.php',array(
			'group_options' => model('#UserGroup','adm_')->getGroupForOptions(),
		));
	}

	public function doUpdate(){
		$user_id = get('user_id',0,'absint');
		$this->checkParam($user_id);
		$user_mod = model('#User','adm_');

		$user = $user_mod->getUserById ( $user_id );
		if(empty($user)){
			return $this->error(ErrorMessage::USER_NOT_EXIST,"os/users");
		}
		if (is_post()) {
			$user_name = $real_name = $mobile = $password = $email = $user_desc = $user_group = '';
			extract ( $_POST, EXTR_IF_EXISTS );
			if($real_name=="" || $mobile =="" || $email =="" || ($user_id !=1 && $user_group <= 0) ){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$update_data = array ('real_name' => $real_name, 'mobile' => $mobile,
									'email' => $email, 'user_desc' => $user_desc ,'password'=>$password);
				if($user_id > 1 ){
					  $update_data["user_group"]=$user_group;
				}
				
				$result = $user_mod->updateUser ( $user_id,$update_data );
				
				if ($result>=0) {
					$current_user=session('user_info');
					$ip = Common::getIp();
					$update_data['ip']=$ip;
					unset($update_data['password']);
					$this->sysLog($current_user['user_name'], 'MODIFY', 'User' , $user_id, json_encode($update_data) );
					return $this->success ('更新完成','os/users');
				} else {
					$this->alert("error",'os/users');
				}
			}
		}

		$this->display('update.php',array(
			'user'=>$user,
			'group_options'=>model('#UserGroup','adm_')->getGroupForOptions(),
		));
	}

	public function doDelete(){
		$user_id = get('user_id',0,'absint');
		$this->checkParam($user_id);
		$user_info = session('user_info');
		$user_mod = model('#User','adm_');
		if($user_id == $user_info['user_id']){
			return $this->error(ErrorMessage::CAN_NOT_DO_SELF);
		}else{
			if($user_id==1){
				return $this->success ('不能删除初始管理员','os/users');
			}
			$user = $user_mod->getUserById($user_id);
			$result = $user_mod->delUser ( $user_id );
			if ($result>=0) {
				$user['password']=null;
				model('#SysLog','adm_')->addLog ($user_info['user_name'], 'DELETE',  'User' ,$user_id ,json_encode($user) );
				return $this->success ( '已删除','os/users' );
			}else{
				return $this->error("error",'os/users');
			}
		}
	}
}